package com.zlt.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zlt.dao.DeliveryDao;
import com.zlt.dao.Impl.DeliveryDaoImpl;
import com.zlt.dao.Impl.JobDaoImpl;
import com.zlt.dao.Impl.UserDaoImpl;
import com.zlt.dao.JobDao;
import com.zlt.dao.UserDao;
import com.zlt.dto.DeliveryDTO;
import com.zlt.entity.*;
import com.zlt.service.DeliveryService;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.StringUtil;
import com.zlt.util.WordReporterUtil;
import com.zlt.util.previewResumeUtil;
import com.zlt.vo.ActiveUser;
import com.zlt.vo.DeliveryVO;
import com.zlt.vo.Pager;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author IBing
 * @Date 2022/8/5 15:12
 * @Version 1.0
 */
public class DeliveryServiceImpl implements DeliveryService {
    private DeliveryDao deliveryDao = new DeliveryDaoImpl();
    private JobDao jobDao = new JobDaoImpl();
    private UserDao userDao = new UserDaoImpl();

    @Override
    public Pager<DeliveryVO> selectJobResume(Pager<DeliveryVO> pager, DeliveryDTO deliveryDTO) {
//        //获取登录的HR
        ActiveUser activeUser = ActiveUserUtil.get();
//        获取当前HR 关联的企业id
        Company company = deliveryDao.selectCompanyByUid(activeUser.getUid());
//        System.out.println(company);

        //获取搜索的参数
        JSONObject object =null;
        if( JSONObject.parseObject(deliveryDTO.getSearchParams()) !=null){
            object= JSONObject.parseObject(deliveryDTO.getSearchParams());
        }
        String nickname =null;
        String jtid =null;
        String state =null;
        try {
            if (object!=null&& StringUtil.isNotNull((object.getString("nickname")))) {
                nickname = object.getString("nickname");
            }
            if (object!=null&& StringUtil.isNotNull(object.getString("jtid")))
                jtid= object.getString("jtid");
            if (object!=null&& StringUtil.isNotNull(object.getString("state")))
                state = object.getString("state");
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        System.out.println(jtid);
        System.out.println(nickname);
        System.out.println(state);

        //获取 当前企业所有投递记录
        List<Delivery> deliverys = new ArrayList<>();
        if (state!=null){
            System.out.println(Integer.valueOf(state));
           deliverys = deliveryDao.selectJobResume(company.getCid(), Integer.valueOf(state));
        }else {
            deliverys = deliveryDao.selectJobResume(company.getCid());
        }
        List<DeliveryVO> deliveryVOS = new ArrayList<>();
        deliverys.forEach( d -> {
            DeliveryVO deliveryVO = new DeliveryVO();
            deliveryVOS.add(deliveryVO);
            try {
                //获取用户信息
                User user = userDao.selectUserByUid(d.getUser().getUid());
                d.setUser(user);
                //获取岗位信息
                Job job = jobDao.selectJobByJid(d.getJob().getJid());
                //查询岗位的分类名
                JobType jobType = jobDao.selectJobTypeByJtid(job.getJobType().getJtid());
                job.setJobType(jobType);
                d.setJob(job);
                d.setCompany(company);
                BeanUtils.copyProperties(deliveryVO,d);
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        });

//        符合 nickname 条件返回结果集
        List<User> users=new ArrayList<>();
        if (nickname!=null){
            nickname="%"+nickname+"%";
            users=deliveryDao.selectUserByNickname(nickname);
            System.out.println(users);
        }
        List<DeliveryVO> deliveryVOS2 = new ArrayList<>();
        if (nickname ==null && jtid==null){
            for (DeliveryVO deliveryVO:deliveryVOS) {
                deliveryVOS2.add(deliveryVO);
            }
        }else if (nickname ==null && jtid!=null){
            for (DeliveryVO deliveryVO:deliveryVOS) {
                if (deliveryVO.getJob().getJobType().getJtid()==Integer.parseInt(jtid)){
                    deliveryVOS2.add(deliveryVO);
                }
            }
        }else if (nickname !=null && jtid==null){
            for (DeliveryVO deliveryVO:deliveryVOS) {
                for (User user:users) {
                    if (deliveryVO.getUser().getNickname().equals(user.getNickname())){
                        deliveryVOS2.add(deliveryVO);
                    }
                }
            }
        }else {  //
            for (DeliveryVO deliveryVO:deliveryVOS) {
                for (User user:users){
                    if (deliveryVO.getUser().getNickname().equals(user.getNickname())){
                        if (deliveryVO.getJob().getJobType().getJtid()==Integer.parseInt(jtid)){
                            deliveryVOS2.add(deliveryVO);
                        }
                    }
                }
            }
        }
        //输出最终结果
        System.out.println(deliveryVOS2);
        pager.setData(deliveryVOS2);
        return pager;
    }

    @Override
    public boolean updateResumeState(Integer did, Integer state) {
        return deliveryDao.updateResumeState(did,state) > 0;
    }

    @Override
    public boolean updateResumeState(List didList, Integer state) {
        return deliveryDao.updateResumeState(didList,state) > 0;
    }

    @Override
    public Resume onlineViewResume(String uid) {
        Resume resume = deliveryDao.selectResume(uid);
        System.out.println("resume:"+resume);
        return resume;
    }


}
